/**
 * @author Administrador
 * Created on 15/03/2006
 */
package br.unb.statistic.inference.dataReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.StringTokenizer;


public class DataReader {

    private String delimiter = "";
    private String commentary = "\n";
    private LineNumberReader lnr;
    private double lastValue = 0;
    private boolean isFirstValue = true, isLastValue = false;
    private double firstTime = 0;
    private int zerosLeft = 0;
    private long windowNumber = 1;
    private File file;

    public String getCommentary() {
        return commentary;
    }

    public String getDelimiter() {
        return delimiter;
    }

    public File getFile() {
        return file;
    }

    /**
     *
     */
    public DataReader(File file) throws IOException {
        super();
        this.file = file;
        lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
    }

    public DataReader(File file, String commentary) throws IOException {
        super();
        this.commentary = commentary;
        this.file = file;
        lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
    }

    public DataReader(String delimiter, File file) throws IOException {
        super();
        this.delimiter = delimiter;
        this.file = file;
        lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
    }

    public DataReader(String delimiter, File file, String commentary) throws IOException {
        super();
        this.delimiter = delimiter;
        this.commentary = commentary;
        this.file = file;
        lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
    }

    public String[] readLine() throws IOException {
        String result[] = null;
        String line;
        while (isCommentary(line = lnr.readLine()));
        if (line != null) {
            StringTokenizer st = new StringTokenizer(line, delimiter);
            int length = st.countTokens();
            result = new String[length];
            for (int i = 0; i < length; i++) {
                result[i] = st.nextToken();
            }
        }
        return result;
    }

    public String nextValueAt(int columnNumber) throws IOException {
        String result = null;
        String line;
        while (isCommentary(line = lnr.readLine()));
        if (line != null) {
            StringTokenizer st = new StringTokenizer(line, delimiter);
            if (st.countTokens() < columnNumber) {
                return " ";
            }
            for (int i = 0; i < columnNumber; i++) {
                st.nextToken();
            }
            result = st.nextToken();
        }
        return result;
    }

    private boolean isCommentary(String line) {
        if (line == null) {
            return false;
        }
        return line.startsWith("" + this.commentary) || line.equals("");
    }

    public String nextInterPacketValueAt(int columnNumber) throws IOException {
        if (this.isFirstValue) {
            this.isFirstValue = false;
            this.nextInterPacketValueAt(columnNumber);
        }
        String result = null;
        String nextString;
        double value = 0;
        while ((nextString = this.nextValueAt(columnNumber)) != null) {
            try {
                value = java.lang.Double.parseDouble(nextString);
                result = "" + Math.abs((value - lastValue));
                lastValue = value;
                break;
            } catch (NumberFormatException nfe) {
            }
        }
        return result;
    }

    public String nextPacketsPerTimeValueAt(int columnNumber, double unitTime) throws IOException {
        if (isLastValue) {
            return null;
        }
        String result = null;
        double time;
        long numPackets = 1;
        if (this.zerosLeft > 0) {
            this.zerosLeft--;
            return "" + 0;
        }
        String nextString;
        if (isFirstValue) {
            isFirstValue = false;
            while ((nextString = this.nextValueAt(columnNumber)) != null) {
                try {
                    firstTime = java.lang.Double.parseDouble(nextString);
                    break;
                } catch (NumberFormatException nfe) {
                }
            }
        }
        while ((nextString = this.nextValueAt(columnNumber)) != null) {
            try {
                time = java.lang.Double.parseDouble(nextString);
                if (time - firstTime < windowNumber * (unitTime)) {
                    numPackets++;
                } else {
                    result = "" + numPackets;
                    windowNumber++;
                    long i = windowNumber;
                    long temp = (long) ((time - firstTime) / (unitTime));
                    for (; i <= temp; i++) {
                        zerosLeft++;
                        windowNumber++;
                    }
                    break;
                }
            } catch (NumberFormatException nfe) {
            }
        }
        if (result == null) {
            result = "" + numPackets;
            isLastValue = true;
        }
        return result;
    }

    public void close() throws IOException {
        lnr.close();
    }

    /*
    public static void main(String args[]) {
    try {
    LogFile file = new LogFile("C:\\Documents and Settings\\hp\\My Documents\\Projeto\\Table_Global_Bandwidth_per_Service_1min_DN_Wed_Aug_16_14_36_26_GMT-03_00_2006.csv");
    LogFile ftp, browsing, email, mgcp, gTcp, gUdp, gIp, news, p2p, sip, h323, stream, donkey, kazaa, bittorrent, commfileshare, winny, gnutella, winmx, skype, instmessage, game, netadmin, voip, voipbrt, voipgvt;
    ftp = new LogFile("ftp.dt");
    browsing = new LogFile("browsing.dt");
    email = new LogFile("email.dt");
    mgcp = new LogFile("mgcp.dt");
    gTcp = new LogFile("gTcp.dt");
    gUdp = new LogFile("gUdp.dt");
    gIp = new LogFile("gIp.dt");
    news = new LogFile("news.dt");
    p2p = new LogFile("p2p.dt");
    sip = new LogFile("sip.dt");
    h323 = new LogFile("h323.dt");
    stream = new LogFile("stream.dt");
    donkey = new LogFile("donkey.dt");
    kazaa = new LogFile("kazaa.dt");
    bittorrent = new LogFile("bittorrent.dt");
    commfileshare = new LogFile("commfileshare.dt");
    winny = new LogFile("winny.dt");
    gnutella = new LogFile("gnutella.dt");
    winmx = new LogFile("winmx.dt");
    skype = new LogFile("skype.dt");
    instmessage = new LogFile("instmessage.dt");
    game = new LogFile("game.dt");
    netadmin = new LogFile("netadmin.dt");
    voip = new LogFile("voip.dt");
    voipbrt = new LogFile("voipbrt.dt");
    voipgvt = new LogFile("voipgvt.dt");
    LogFile total = new LogFile("total.dt");
    DataReader dr = new DataReader(",", file, "#");
    String line[], line0 = null;
    double n = 0;
    while ((line = dr.readLine()) != null) {
    if (line0 == null) {
    line0 = line[0];
    n += Double.parseDouble(line[1]);
    } else {
    if (line0.equals(line[0])) {
    n += Double.parseDouble(line[1]);
    } else {
    total.write(line0 + "\t" + n);
    line0 = line[0];
    n = 0;
    }
    }
    if (line[2].equals("FTP")) {
    ftp.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Browsing*")) {
    browsing.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("E-Mail*")) {
    email.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("MGCP")) {
    mgcp.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Generic TCP")) {
    gTcp.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Generic UDP")) {
    gUdp.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Generic IP")) {
    gIp.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Newsgroups")) {
    news.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("P2P")) {
    p2p.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("SIP")) {
    sip.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("H323")) {
    h323.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Streaming*")) {
    stream.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("eDonkey/eMule")) {
    donkey.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Kazaa")) {
    kazaa.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Bittorrent")) {
    bittorrent.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Commercial LogFile Sharing")) {
    commfileshare.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Winny")) {
    winny.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Gnutella")) {
    gnutella.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("WinMX")) {
    winmx.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Skype")) {
    skype.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Instant Messaging")) {
    instmessage.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Gaming")) {
    game.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("Net Admin")) {
    netadmin.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("VoIP")) {
    voip.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("VoIP_BrT")) {
    voipbrt.write(line[0] + "\t" + line[1]);
    } else if (line[2].equals("VoIP_GVT")) {
    voipgvt.write(line[0] + "\t" + line[1]);
    }
    }
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    
     */
}
